import { Component, OnInit, Input } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { PersonnelInfo } from 'src/app/models';
import { PersonnelService } from 'src/app/services/personnel.service';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
@Component({
  selector: 'zepride-account-modal',
  templateUrl: './account-modal.component.html',
  styleUrls: ['./account-modal.component.less']
})
export class AccountModalComponent implements OnInit {
  @Input() UserInfo;
  @Input() orgTreeId = '';
  password: string; //密码
  personnelInfo: PersonnelInfo = { orgTree: { id: '' }, acc: { id: '', password: '' } } as PersonnelInfo;
  constructor(
    private modalRef: NzModalRef<AccountModalComponent>,
    private msg: NzMessageService,
    private personnelService: PersonnelService,
  ) { }

  ngOnInit(): void {
  }
  cancel() {
    this.modalRef.close();
  }

  confirm() {
    this.modalRef.close(true);
  }
  async upPwd() {
    if (!this.password) {
      this.msg.create('error', '请填写密码');
      return;
    }
    this.personnelInfo.id = this.UserInfo.id;
    this.personnelInfo.orgTree.id = this.orgTreeId;
    this.personnelInfo.acc.id = this.UserInfo.accountId;
    this.personnelInfo.acc.password = this.password;
    this.personnelInfo.name = this.UserInfo.name;
    this.personnelInfo.employeeNo = this.UserInfo.employeeNo;
    this.personnelInfo.organizationId = this.UserInfo.organizationId;
    this.personnelInfo.accountId = this.UserInfo.accountId;
    try {
      await this.personnelService.updateUser(this.personnelInfo);
      this.msg.success('修改账号信息成功');
      this.confirm();
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }
}
